package com.abbas.abchat.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.abbas.abchat.R;
import com.abbas.abchat.adapter.ReplayAdapter;
import com.abbas.abchat.entity.Replay;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.utils.SpUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SetReplayActivity extends BaseActivity {

    private ImageButton ibBack;
    private Button finish;
    private ListView lv;
    private TextView tag;
    private RelativeLayout add;

    private Account account;

    private ReplayAdapter adapter;
    private List<Replay> replayList = new ArrayList<>();

    private SpUtils spUtils;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_replay;
    }

    @Override
    protected void init() {

        account = (Account) getIntent().getSerializableExtra("account");

        if (account == null) showMessage("数据错误");

        ibBack = (ImageButton) findViewById(R.id.ib_back);
        finish = (Button) findViewById(R.id.finish);
        tag = (TextView) findViewById(R.id.tag);
        lv = (ListView) findViewById(R.id.lv);

        spUtils = SpUtils.config(this,"s"+account.getId());
        Map<String, String> data = spUtils.getAll();

        if (data.size() <= 0){
            showMessage("没有自动回复数据");
            if (replayList != null){
                replayList.add(new Replay("",""));
            }
        }else {
            Set<String> keySet = data.keySet();
            Iterator<String> iterator = keySet.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                replayList.add(new Replay(next,data.get(next)));
            }
        }

        View footView = LayoutInflater.from(this).inflate(R.layout.replay_foot,null,false);
        add = (RelativeLayout) footView.findViewById(R.id.add);

        adapter = new ReplayAdapter(this,replayList);
        lv.setAdapter(adapter);
        lv.addFooterView(footView);

    }

    @Override
    protected void initEvent() {
        //TODO：新增关键词布局
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (replayList != null){
                    replayList.add(new Replay("",""));
                }
                adapter.notifyDataSetChanged();
            }
        });

        //TODO：完成新增
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpUtils spUtils = SpUtils.config(SetReplayActivity.this,"s"+account.getId());
                for (Replay replay : replayList) {
                    if (!TextUtils.isEmpty(replay.getKey()) && !TextUtils.isEmpty(replay.getValue())) {
                        spUtils.putString(replay.getKey(),replay.getValue());
                    }else {
                        showMessage("关键词与回复不能为空");
                    }
                }
                finishThis();
            }
        });
    }
}